package com.woniuxy.rbac.web.api;

import com.woniuxy.rbac.entity.Perm;
import com.woniuxy.rbac.entity.ResponseResult;
import com.woniuxy.rbac.service.PermService;
import com.woniuxy.rbac.utils.Audience;
import com.woniuxy.rbac.utils.JwtUtil;
import com.woniuxy.rbac.vo.PermVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: L
 */
@RestController
@RequestMapping("/perm")
public class PermController {
    @Autowired
    private PermService permService;
    @Autowired
    private Audience audience;

    @GetMapping("getForUser")
    public ResponseResult<List<Perm>> getPermsById(@RequestHeader("token") String jwt) {
        int userId = JwtUtil.parseJwtForId(jwt, audience);
        return new ResponseResult<>(permService.getPermsByUserId(userId));
    }
    @GetMapping("getMenu")
    public ResponseResult<List<Perm>> getPermsMenu(@RequestHeader("token") String jwt) {
        int userId = JwtUtil.parseJwtForId(jwt, audience);
        return new ResponseResult<>(permService.getPermsMenu(userId));
    }

    @GetMapping("getall")
    public ResponseResult<List<PermVO>> getAllPerms(){
        List<Perm> perms = permService.getAll();
        List<PermVO> permVOs = new ArrayList<>();

        perms.forEach(p->{
            if(p.getParentid()==0){   //顶级菜单/权限
                PermVO permVO=new PermVO();
                permVO.setId(p.getId());
                permVO.setLabel(p.getName());
                permVO.setChildren(new ArrayList<PermVO>());
                permVOs.add(permVO);
            }
        });

        permVOs.forEach(pvo->{
            perms.forEach(p->{
                if(p.getParentid()==pvo.getId()){  //子菜单
                    PermVO permVO=new PermVO();
                    permVO.setId(p.getId());
                    permVO.setLabel(p.getName());
                    pvo.getChildren().add(permVO);
                }
            });
        });



        return new ResponseResult<>(permVOs);
    }

    @GetMapping("all")
    public ResponseResult<List<PermVO>> getAll(){
        List<Perm> perms = permService.getAll();
        List<PermVO> permVOs = new ArrayList<>();

        perms.forEach(p -> {
            if (p.getParentid() == 0) {   //顶级菜单/权限
                PermVO permVO = new PermVO();
                permVO.setId(p.getId());
                permVO.setLabel(p.getName());
                permVO.setType(p.getType());
                permVO.setChildren(new ArrayList<PermVO>());
                permVOs.add(permVO);
            }
        });

        permVOs.forEach(pvo -> {
            perms.forEach(p -> {
                if (p.getParentid() == pvo.getId()) {  //子菜单
                    PermVO permVO = new PermVO();
                    permVO.setId(p.getId());
                    permVO.setLabel(p.getName());
                    permVO.setType(p.getType());
                    pvo.getChildren().add(permVO);
                }
            });
        });
        return new ResponseResult<>(permVOs);
    }
}
